﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace cjwygl
{
    public partial class FrmShuidianMgr : Form
    {
        public List<ComboItem> collection = new List<ComboItem>();
        public FrmShuidianMgr()
        {
            InitializeComponent();
            InitializeComboSearch();
        }
        public void InitializeComboSearch()
        {
            collection.Add(new ComboItem { Text = "所有列", Value = "ALL" });
            string valuetext="";
            for (int i = 0; i < dataGridView1.Columns.Count; i++)
            {
                if (dataGridView1.Columns[i].DataPropertyName == "COMPANY_NAME")
                {
                    valuetext = "a." + dataGridView1.Columns[i].DataPropertyName;
                }
                else
                {
                    valuetext = "b." + dataGridView1.Columns[i].DataPropertyName;
                }
                collection.Add(new ComboItem
                {
                    Text = dataGridView1.Columns[i].HeaderText,
                    Value = valuetext
                });
            }
            comboBox_cols.DisplayMember = "Text";
            comboBox_cols.ValueMember = "Value";
            comboBox_cols.DropDownStyle = ComboBoxStyle.DropDownList;
            comboBox_cols.DataSource = collection;
            comboBox_cols.SelectedValue = "ALL";

        }
        private void LoadData()
        {
            using (DataBase db = new DataBase())
            {
                DataTable dt = db.ExecuteDataTable("SELECT a.COMPANY_NAME, b.ID, b.FEE_TYPE, b.CB_NUM, b.CB_TIME," +
                            " b.CB_FEE, b.CB_EMPLOYEE, b.CB_COMMENT, b.OP_NAME, b.OP_ID, " +
                            " b.LOG_TIME, b.MODIFY_COMMENT, b.LAST_MODUFY_DATE " +
                            "FROM CJ_CUSTOMER_INFO a INNER JOIN " +
                            " CJ_MISC_FEE b ON a.ID = b.COMPANY_ID");
                this.viewSINGLEFEEBindingSource.DataSource = dt;
            }
            // TODO: 这行代码将数据加载到表“cjwyglDataSet_SINGLEFEE.View_SINGLE_FEE”中。您可以根据需要移动或删除它。
            this.view_SINGLE_FEETableAdapter.Fill(this.cjwyglDataSet_SINGLEFEE.View_SINGLE_FEE);


        }
        private void LoadData(DataTable dt)
        {
            this.viewSINGLEFEEBindingSource.DataSource = dt;
            this.view_SINGLE_FEETableAdapter.Fill(this.cjwyglDataSet_SINGLEFEE.View_SINGLE_FEE);
        }
        private void FrmShuidianMgr_Load(object sender, EventArgs e)
        {
            LoadData();
        }

        private void button_add_Click(object sender, EventArgs e)
        {
            FrmShuidian frmAdd = new FrmShuidian();
            frmAdd.ShowDialog();
        }

        private void button_refresh_Click(object sender, EventArgs e)
        {

            LoadData();
        }

        private void button_modify_Click(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedRows.Count == 0)
            {
                MessageBox.Show("未选中任何行");
                return;
            }
            else
            {
                int id = -1;
                int.TryParse(dataGridView1.SelectedRows[0].Cells[0].Value.ToString(), out id);
                FrmShuidian frmupdate = new FrmShuidian(id);
                frmupdate.ShowDialog();

            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            DialogResult dr = MessageBox.Show("确定要删除吗？", "删除该条记录", MessageBoxButtons.OKCancel);
            if (dr == DialogResult.Cancel)
            {
                return;
            }
            if (dataGridView1.SelectedRows.Count == 0)
            {
                MessageBox.Show("未选中任何行");
                return;
            }
            else
            {
                int id = -1;
                int.TryParse(dataGridView1.SelectedRows[0].Cells[0].Value.ToString(), out id);
                if (id == -1)
                {
                    MessageBox.Show("系统错误，未识别的行");
                }
                else
                {
                    using (DataBase db = new DataBase())
                    {
                        string sql = "delete from CJ_MISC_FEE where ID=@ID";
                        db.AddParameter("ID", id);
                        int ret = db.ExecuteNonQuery(sql);
                        MessageBox.Show("已删除" + ret + "行");
                        LoadData();
                    }
                }

            }
        }

        private void button_search_Click(object sender, EventArgs e)
        {
            string sql = "";
            string key = textBox_search.Text.Trim();
            if (key == "")
            {
                MessageBox.Show("请输入查询关键词");
                return;
            }
            if (comboBox_cols.SelectedValue.ToString() == "ALL")
            {
                StringBuilder sb = new StringBuilder();
                sb.Append("SELECT a.COMPANY_NAME, b.ID, b.FEE_TYPE, b.CB_NUM, b.CB_TIME,"+ 
                            " b.CB_FEE, b.CB_EMPLOYEE, b.CB_COMMENT, b.OP_NAME, b.OP_ID, "+
                            " b.LOG_TIME, b.MODIFY_COMMENT, b.LAST_MODUFY_DATE "+
                            "FROM CJ_CUSTOMER_INFO a INNER JOIN " +
                            " CJ_MISC_FEE b ON a.ID = b.COMPANY_ID");
                sb.Append(" WHERE 1=2 ");
                for (int i = 1; i < collection.Count; i++)
                {
                    sb.Append("or " + collection[i].Value + " like '%" + key + "%' ");
                }
                sql = sb.ToString();
            }
            else
            {
                sql = "SELECT a.COMPANY_NAME, b.ID, b.FEE_TYPE, b.CB_NUM, b.CB_TIME,"+ 
                            " b.CB_FEE, b.CB_EMPLOYEE, b.CB_COMMENT, b.OP_NAME, b.OP_ID, "+
                            " b.LOG_TIME, b.MODIFY_COMMENT, b.LAST_MODUFY_DATE "+
                            "FROM CJ_CUSTOMER_INFO a INNER JOIN " +
                            " CJ_MISC_FEE b ON a.ID = b.COMPANY_ID where " +
                    comboBox_cols.SelectedValue + " like '%" + key + "%' ";
            }
            using (DataBase db = new DataBase())
            {
                DataTable dt = db.ExecuteDataTable(sql);
                LoadData(dt);
            }
        }
    }
}
